package com.huang.controller;

import com.huang.pojo.Menu;
import com.huang.pojo.Role;
import com.huang.service.RoleService;
import com.huang.vo.PageResult;
import com.huang.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
@ResponseBody
public class RoleController {

    @Resource
    private RoleService roleService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public Result list(String r_name, Integer pageIndex, Integer pageSize){
        PageResult list = roleService.list(r_name, pageIndex, pageSize);
        Result result = new Result(200,"查询成功",null,list);
        return result;
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable("id") Integer id){
        Role role = roleService.findById(id);
        Result result;
        if (role == null)   result = new Result(200,"error","没有该信息",null);
        else result = new Result(200,"success","查询成功",role);
        return result;
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") Integer id){
        int delete = roleService.delete(id);
        Result result;
        if (delete != 1) result = new Result(200,"error","删除失败",null);
        else result = new Result(200,"success","删除成功",null);
        return result;
    }

    @RequestMapping(value = "/batch/{ids}",method = RequestMethod.DELETE)
    public Result deleteByBatch(@PathVariable("ids") String ids){
        String[] split = ids.split(",");
        int delete = roleService.delete(split);
        Result result;
        if (delete < 1) result = new Result(200,"error","删除失败",null);
        else result = new Result(200,"success","删除成功",null);
        return result;
    }
    @RequestMapping(value = "/isExistName/{r_name}",method = RequestMethod.GET)
    public Result isExistName(@PathVariable("r_name") String name){
        Role role = roleService.isExistName(name);
        Result result;
        if (role != null) {
            result = new Result(200, "资源名称已存在，请重新输入", "success", role);
        } else result = new Result(200, "", "fail", null);
        return result;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result insert(Role role) {
        int insert = roleService.insert(role);
        Result result;
        if (insert >= 1) {
            result = new Result(200, null, "success", null);
        } else result = new Result(200, null, "fail", null);
        return result;
    }
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public Result update(Role role) {
        int update = roleService.update(role);
        Result result;
        if (update >= 1) {
            result = new Result(200, "success", null, null);
        } else result = new Result(200, "fail",null, null);
        return result;
    }
    // 查询所有role
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public Result getAll() {
        List<Role> roles = roleService.getAll();
        Result result;
        result = new Result(200, "success", "查询成功", roles);
        return result;
    }
}
